import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home/Home.vue'
import HomePage from '@/views/Home/HomePage/HomePage.vue'
import GoodsPage from '@/views/Home/GoodsPage/GoodsPage.vue'
import MapPosition from '@/components/HomeInside/VueAmap/MapPosition.vue'
import Order from '@/views/Order/Order.vue'
import View from '@/components/OrderInside/ViewOrder.vue'
import Detail from '@/components/OrderInside/OrderDetail.vue'
import PayState from '@/components/OrderInside/PayState.vue'
import Account from '@/views/Account/Account.vue'
import Card from '@/components/AccountInside/BankCard/BankCard.vue'
import AddCard from '@/components/AccountInside/BankCard/AddCard.vue'
import Address from '@/components/AccountInside/Address/Address.vue'
import AddEdit from '@/components/AccountInside/Address/AddEdit.vue'
import Coupon from '@/components/AccountInside/Coupon/Coupon.vue'
import Settings from '@/components/AccountInside/Settings/Settings.vue'

import Login from '@/views/Login/Login.vue'
import RegPage from '@/views/Login/RegPage.vue'
import LogPage from '@/views/Login/LogPage.vue'
import store from '@/store/index.js'

Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: '/home',
    component: Home,
    redirect: '/home/homepage',
    children: [
      { path: 'homepage', component: HomePage, meta: { title: '首页', show: false } }, // 给路由组件一个title名，展示在浏览器标签上
      { path: 'goodspage/:id', component: GoodsPage, props: true, meta: { title: '商品', show: true } },
      { path: 'position', component: MapPosition },
      { path: 'order', component: Order, meta: { title: '订单', show: true } },
      { path: 'view', component: View },
      { path: 'detail', component: Detail },
      { path: 'paystate', component: PayState },
      { path: 'payments', component: Card },
      { path: 'account', component: Account, meta: { title: '账户', show: true } },
      { path: 'card', component: Card },
      { path: 'addCard', component: AddCard },
      { path: 'address', component: Address },
      { path: 'addedit', component: AddEdit },
      { path: 'coupon', component: Coupon },
      { path: 'settings', component: Settings }
    ]
  },
  {
    path: '/login',
    component: Login,
    redirect: '/login/logpage',
    children: [
      { path: 'logpage', component: LogPage },
      { path: 'regpage', component: RegPage }
    ]
  },
  { path: '/', redirect: '/login' }
]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  document.title = to.meta.title // 给每个路由组件一个title名，展示在浏览器标签上
  const token = localStorage.getItem(store.state.telInput)
  const admin1 = localStorage.getItem('+8618980082791CN')
  const admin2 = localStorage.getItem('+8618224420928CN')
  const pathArr = ['/home/homepage', '/home/goodspage/:id', '/home/position', '/home/order', '/home/account']
  if (pathArr.indexOf(to.path) !== -1) {
    if (token || admin1 || admin2) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
